#include <iostream>
#include <queue>
#include <map>
#include <string>
using namespace std;
typedef queue<int> QUE;
QUE q;
QUE tq[1000+5];
map<int, int> id;
int main() {
    ios_base::sync_with_stdio(false);cin.tie(nullptr);
    int N, cnt = 0;
    while(cin >> N && N){
        ++cnt;
        cout << "Scenario #" << cnt << "\n";
        for (int i = 1; i <= N; ++i) {
            int m; cin >> m;
            for (int j = 0; j < m; ++j) {
                int ele; cin >> ele;
                id[ele] = i;
            }
        }
        QUE emp;
        q = emp; for (int i = 1; i <= N; ++i) tq[i] = emp;
        string in;
        while(cin >> in && in[0]!='S'){
            if(in[0] == 'E'){
                int t; cin >> t;
                int team = id[t];
                if(tq[team].empty()){
                    q.push(team);
                }
                tq[team].push(t);
            }
            else if(in[0] == 'D'){
                int first = q.front();
                cout << tq[first].front() << "\n";
                tq[first].pop();
                if(tq[first].empty()) q.pop();
            }
        }
        cout << "\n";
    }
    return 0;
}